package com.bw.app;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * @BelongsProject: gmall_2210B
 * @BelongsPackage: com.bw.app
 * @Author: weiliujia
 * @CreateTime: 2025-06-27
 * @Description: TODO
 * @Version: 1.0
 */
public class HBaseQueryExample {
    public static void main(String[] args) throws Exception {
        // 创建配置并连接 HBase
        Configuration config = HBaseConfiguration.create();
        try (Connection connection = ConnectionFactory.createConnection(config);
             Table table = connection.getTable(TableName.valueOf("gmall_0620:dim_activity_info"))) {

            // 查询单行数据
            Get get = new Get(Bytes.toBytes("row_key"));
            Result result = table.get(get);

            // 处理结果
            for (Cell cell : result.listCells()) {
                String family = Bytes.toString(CellUtil.cloneFamily(cell));
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(family + ":" + qualifier + " = " + value);
            }

            // 扫描多行数据
            Scan scan = new Scan();
            try (ResultScanner scanner = table.getScanner(scan)) {
                for (Result res : scanner) {
                    System.out.println("Row: " + Bytes.toString(res.getRow()));
                }
            }
        }
    }
}
